// pages/group/members/index.ts
import tran from '../../../utils/language'
import { userTargetInfo } from '../../../utils/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _tBase: tran._t('base'),
    userData: [{
      userId: '324242',
      nickname: 'Benjamin Smith',
      avatarurl: 'https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg',
      personalityAvatar: '', // mbti性格的图标，现在后端那边还没设置进去，所以目前查到的没有这个属性，等java那边加上了就有了
    }],
    info: {
      groupId: ''
    },
    downPopupParams: {
      showDownPopup: false
    },
    homeFriendInfo: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options: any) {
    this.setData({
      userData: []
    })
    if (options.info) {
      this.setData({
        info: JSON.parse(options.info)
      }, () => {
        this.getImData()
      })
    }
  },
  closeFriendInfoPopup () {
    const { downPopupParams } = this.data
    downPopupParams.showDownPopup = false
    this.setData({
      downPopupParams
    })
  },
  /**
   * 点击解锁个人资料
   */
  goFriendInfo(e: any) {
    const { info } = e.currentTarget.dataset
    const params = {
      targetUserId: info.userId
    }
    userTargetInfo(params).then(res => {
      const { downPopupParams } = this.data
      downPopupParams.showDownPopup = true
      res.addAndSend = true
      res.jumpType = 5
      this.setData({ downPopupParams: downPopupParams, homeFriendInfo: res, showEventPopup: false })
    })
  },
  closeDownPopup() {
    const { downPopupParams } = this.data
    downPopupParams.showDownPopup = false
    this.setData({ downPopupParams: downPopupParams })
  },
   /**
   * 获取IM是否登录
   */
  async getImData () {
    const that = this
    let wxIm = wx.conn
    // 判断是否IM连接成功
    if (wxIm) {
      await this.getList()
      // 此处表示已查到im,就停止
      return;
    } else {
      // 如果没有wx.im，说明im还没连接成功，就再次调用getImData方法继续查
      setTimeout(() => {
        that.getImData()
      }, 500)
    }
  },
  getList() {
    const { info } = this.data
    const that = this
    let option = {
      pageNum: 1,
      pageSize: 1000,
      groupId: info.groupId,
    }
    wx.showLoading({
      title: '加载中...'
    })
    wx.conn.listGroupMembers(option).then((res: any) => {
      wx.hideLoading()
      that.getuserAttribute(res.data)
    });
  },
  /**
   * 获取用户属性
   * @param arr 
   */
  getuserAttribute(arr: any) {
    let list: any = []
    arr.length > 0 && arr.map((item: any) => {
      if (item.member) {
        list.push(item.member)
      }
    })
    let userList: any = JSON.parse(JSON.stringify(list))
    wx.conn.fetchUserInfoById(list).then((res: any) => {
     let allUserLst: any = []
      userList.map((item: any) => {
        const obj1 = res.data[item]
        const extData = obj1.ext ? JSON.parse(obj1.ext) : {}
        const obj = {
          userId: item,
          ...obj1,
          ...extData
        }
        allUserLst.push(obj)
      })
      this.setData({
        userData: allUserLst
      })
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})